﻿using LDM.Admin.Models.Login;
using LDM.Framework.NormalizeActionResult;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request;
using LDM.Sys.ServiceAPI.Svc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace LDM.Admin.Controllers
{
    public class LoginController : Controller
    {
        private readonly IAdminLoginService _adminLoginService;
        private readonly ILoginService _loginService;

        public LoginController(
            IAdminLoginService adminLoginService,
            ILoginService loginService)
        {
            _adminLoginService = adminLoginService;
            _loginService = loginService;
        }
        /// <summary>
        /// 登录页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public JsonResult WebUserLogin([FromBody] UserLoginRequest request)
        {
            _loginService.GetAbnormal();
            var userInfo = _adminLoginService.Login(new AdminLoginReq
            {
                UserName = request.UserName,
                Password = request.UserPwd
            }).Result;
            if (userInfo != null && !string.IsNullOrEmpty(userInfo.Token))
                return Json(ResultData.Success("登录成功", userInfo));
            else
                return Json(ResultData.Error("登陆失败",null));

        }
    }
}
